package com.mach.platform.api.service;

import com.mach.platform.api.domain.Attention;
import com.mach.platform.api.repository.ConcernRepo;
import com.mach.platform.service.BaseService;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 关注业务类
 */

@Service
public class ConcernServiceImpl implements BaseService<Attention> {

    @Autowired
    private ConcernRepo concernRepo;

    @Override
    public Attention save(Attention attention) {
        if (attention != null){
            attention.setAttentTime(LocalDateTime.now());
            return concernRepo.save(attention);
        }
        return null;
    }

    @Override
    public List<Attention> insertObjInBatch(List<Attention> list) {
        return null;
    }

    @Override
    public Attention update(Attention attention) {
        return null;
    }

    /**
     * 取消关注
     * @param ids 批量删除id
     * @return
     */
    @Override
    public int delete(Long[] ids) {
        if (ids != null && ids.length > 0) {
            int result = concernRepo.deleteByIdIn(ids);
            return result;
        }
        return 0;
    }

    /**
     * 关注列表
     * @param pageable 分页参数
     * @param params 其他参数
     * @return
     */
    @Override
    public Page<Attention> list(Pageable pageable, Map params) {

        if (params != null && params.size() > 0) {
            //用户id
            Object userId = params.get("userId");
            if (null != userId && "" != userId) {
                params.put("userId_eq", Integer.parseInt(userId.toString().trim()));
                params.remove("userId");

                Specification specification = DynamicSpecifications.bySearchFilter(SearchFilter.parse(params).values(), Attention.class);
                Page<Attention> result = concernRepo.findAll(specification, pageable);
                return result;
            }
        }
        return null;
    }

    @Override
    public Attention info(Long id) {
        return null;
    }

}
